package com.zhouxujing.game;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Music extends Thread{
	boolean stop = true;
	
public boolean isStop() {
		return stop;
	}

	public void setStop(boolean stop) {
		this.stop = stop;
	}

  public Music() {
	// TODO Auto-generated constructor stub
	  super();
  }
	

	public Music(boolean stop) {
	super();
	this.stop = stop;
}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (stop) {
			try {
	    		AudioInputStream ais = AudioSystem.getAudioInputStream(new File("src/music/BGM.wav"));//绝对路径
	    		AudioFormat aif = ais.getFormat();
	    		final SourceDataLine sdl;
	    		DataLine.Info info = new DataLine.Info(SourceDataLine.class, aif);
	    		sdl = (SourceDataLine) AudioSystem.getLine(info);
	    		sdl.open(aif);
	    		sdl.start();
	    		FloatControl fc = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN);
	    		// value可以用来设置音量，从0-2.0
	    		double value = 2;
	    		float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
	    		fc.setValue(dB);
	    		int nByte = 0;
	    		final int SIZE = 1024 * 64;
	    		byte[] buffer = new byte[SIZE];
	    		while (nByte != -1) {
	    			nByte = ais.read(buffer, 0, SIZE);
	    			sdl.write(buffer, 0, nByte);
	    		}
	    		sdl.stop();
	 
	    	} catch (Exception e) {
	    		e.printStackTrace();
	    	}
		}
	}
	
	public  void  start() {
		
		
		//1 获取你要播放的音乐文件
		try {
			File file = new File("D:\\A\\eclipseworkspace\\feijidazhan\\src\\music\\start.wav");
			//2、定义一个AudioInputStream用于接收输入的音频数据
			AudioInputStream am;
			//3、使用AudioSystem来获取音频的音频输入流(处理（抛出）异常)
			am = AudioSystem.getAudioInputStream(file);
			//4、使用AudioFormat来获取AudioInputStream的格式
			AudioFormat af = am.getFormat();
			//5、一个源数据行
			SourceDataLine sd ;
			//6、获取受数据行支持的音频格式DataLine.info
			//DataLine.Info dl = new DataLine.Info(SourceDataLine.class, af);
			//7、获取与上面类型相匹配的行 写到源数据行里 二选一
			sd = AudioSystem.getSourceDataLine(af);//便捷写法
			//sd = (SourceDataLine) AudioSystem.getLine(dl);
			//8、打开具有指定格式的行，这样可以使行获得资源并进行操作
			sd.open();
			//9、允许某个数据行执行数据i/o
			sd.start();
			//10、写数据
			int sumByteRead = 0; //读取的总字节数
			byte [] b = new byte[320];//设置字节数组大小
			//11、从音频流读取指定的最大数量的数据字节，并将其放入给定的字节数组中。
			while (sumByteRead != -1) {//-1代表没有 不等于-1时就无限读取
				sumByteRead = am.read(b, 0, b.length);//12、读取哪个数组
				if(sumByteRead >= 0 ) {//13、读取了之后将数据写入混频器,开始播放
					sd.write(b, 0, b.length);
				}
			}
			//关闭
			sd.drain();
			sd.close();
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LineUnavailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
		}
}